package Compiler;
import java.util.*;

public class Semantic  {
	 LinkedList<String> newlist = new LinkedList<String>();
	    LinkedList assigned = new LinkedList();
	    int count=0;
	    Terminal currentToken;
	    int i;
	    
	    /**
	     *
	     * @param code
	     * @return newlist
	     */
	    public LinkedList newlist(LinkedList code)
	    {

	        for(i=0; i<code.size(); i++)
	        {
	            currentToken = (Terminal)code.get(i);

	            if(currentToken.getToken().equalsIgnoreCase(Terminal.DOC_BEGIN))
	                newlist.add("<html>");
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.DOC_END))
	                newlist.add("</html>");            
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.COMMENT_BEGIN))
	                comment(code);
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.HEAD_BEGIN))
	                head(code);
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.BEGIN))
	                variable(code);              
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.PARA_BEGIN))
	                paragraph(code);              
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.LIST_BEGIN))
	                list(code);                 
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.BOLD_BEGIN))
	                bold(code);            
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.ITALICS_BEGIN))
	                italics(code);                          
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.SOUND_BEGIN))
	                sound(code);              
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.VIDEO_BEGIN))
	                video(code);               
	            else if(currentToken.getToken().equalsIgnoreCase(Terminal.NEWLINE))
	                newLine(code);          
	        }
	        return newlist;
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void comment(LinkedList code)
	    {
	        newlist.add("<!--"); 
	        i++;
	        currentToken = (Terminal)code.get(i);
	        newlist.add(currentToken.getToken());
	        i++;        
	        newlist.add("-->"); 
	        //i++;
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void head(LinkedList code) 
	    {
	        newlist.add("<head>");
	        i++;
	        newlist.add("<title>");
	        i++;        
	        currentToken = (Terminal)code.get(i);
	        newlist.add(currentToken.getToken());
	        i++;          
	        newlist.add("</title>");
	        i++;
	        newlist.add("</head>");
	        //i++;                     
	    }           
	    
	    /**
	     *
	     * @param code
	     */
	    public void bold(LinkedList code)
	    {
	        newlist.add("<b>");
	        i++;        
	        currentToken = (Terminal)code.get(i);
	        newlist.add(currentToken.getToken());
	        i++;          
	        newlist.add("</b>");
	        //i++;        
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void italics(LinkedList code)
	    {
	        newlist.add("<b>");
	        i++;        
	        currentToken = (Terminal)code.get(i);
	        newlist.add(currentToken.getToken());
	        i++;          
	        newlist.add("</b>");
	        //i++;        
	    }    
	    
	    /**
	     *
	     * @param code
	     */
	    public void sound(LinkedList code)
	    {
	        newlist.add("<audio controls>"); 
	        newlist.add("<source src=\""); 
	        i++;        
	        currentToken = (Terminal)code.get(i);
	        newlist.add(currentToken.getToken());
	        i++;          
	        newlist.add("\">");
	        i++;
	        newlist.add("</audio>");         
	    }    

	    /**
	     *
	     * @param code
	     */
	    public void video(LinkedList code)
	    {
	        newlist.add("<iframe src=\""); 
	        i++;        
	        currentToken = (Terminal)code.get(i);
	        newlist.add(currentToken.getToken());
	        i++;          
	        newlist.add("\"/>");
	        //i++;        
	    }     

	    /**
	     *
	     * @param code
	     */
	    public void newLine(LinkedList code)
	    {
	        newlist.add("<br>"); 
	        //i++;          
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void paragraph(LinkedList code)
	    {
	        newlist.add("<p>"); 
	        i++;          
	        currentToken = (Terminal)code.get(i);
	        
	        if(currentToken.getToken().equalsIgnoreCase(Terminal.BEGIN))
	        {
	            variable(code);
	            i++;
	            currentToken = (Terminal)code.get(i);
	            
	        }
	     
	        while(!currentToken.getToken().equalsIgnoreCase(Terminal.REST_END))
	        {
	                if(currentToken.getToken().equalsIgnoreCase(Terminal.NEWLINE))
	                {
	                    newLine(code);
	                }
	                else 
	                {
	                    innerLP(code);
	                }
	                
	                i++;                
	                currentToken = (Terminal)code.get(i);

	        }       
	        newlist.add("</p>"); 
	        //i++;              
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void list(LinkedList code)
	    {
	        newlist.add("<ul>"); 
	        i++;
	        currentToken = (Terminal)code.get(i);
	        while(!currentToken.getToken().equalsIgnoreCase(Terminal.REST_END))
	        {
	            item(code);
	            i++;
	            currentToken = (Terminal)code.get(i);
	        }
	        newlist.add("</ul>");
	        //i++;
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void item(LinkedList code)
	    {
	        //needs to bed fixed for two innerLP in the same item
	        newlist.add("<li>"); 
	        i++;
	        
	        
	        //this is the part that sorta works
	        currentToken = (Terminal)code.get(i);
	        //System.out.println(currentToken.getToken());
	        innerLP(code);
	        i++;
	        //this is the part that sorta works 
	        

	        /*
	        //added below    
	        currentToken = (Terminal)code.get(i);
	        while(!currentToken.getToken().equalsIgnoreCase(Terminal.MAEK_END))
	        {
	            innerLP(code);
	            i++;
	            currentToken = (Terminal)code.get(i);
	        }
	        //added above 
	        */
	        
	        newlist.add("</li>");               
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void variable(LinkedList code)
	    {
	        Terminal name;
	        Terminal info;
	        i++;
	        name = (Terminal)code.get(i);
	        i++;
	        i++;
	        info = (Terminal)code.get(i);     
	        i++;
	        assigned.add(new variables(name.getToken(), info.getToken()));
	        count++;
	        //i++;
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void use(LinkedList code)
	    {
	        i++;
	        currentToken = (Terminal)code.get(i);
	        variables temp;
	        boolean test = false;    
	        
	        for(int j = 0; j < assigned.size(); j++)
	        {
	            temp = (variables) assigned.get(j);
	            if(currentToken.getToken().equals(temp.getName()))
	            {
	                newlist.add(temp.getInfo());
	                test = true;
	            }
	        }
	        
	        if(test == false)
	        {
	            System.err.println("SEMANTIC ERROR: variable not found : "
	                                + currentToken.getToken());
	            System.exit(0);
	        }
	        
	        i++;
	    }
	    
	    /**
	     *
	     * @param code
	     */
	    public void innerLP(LinkedList code) {
	        if(currentToken.getToken().equalsIgnoreCase(Terminal.BOLD_BEGIN))
	            bold(code);
	        else if (currentToken.getToken().equalsIgnoreCase(Terminal.ITALICS_BEGIN))
	            italics(code);        
	        else if (currentToken.getToken().equalsIgnoreCase(Terminal.LIST_BEGIN))
	            list(code);
	        else innerText(code);  
	        }
	    
	    /**
	     *
	     * @param code
	     */
	    public void innerText(LinkedList code){
	        if(currentToken.getToken().equalsIgnoreCase(Terminal.CALL))
	            use(code);
	        else if (currentToken.getToken().equalsIgnoreCase(Terminal.SOUND_BEGIN))
	            sound(code);        
	        else if (currentToken.getToken().equalsIgnoreCase(Terminal.VIDEO_BEGIN))
	            video(code);
	        else if (currentToken.getToken().equalsIgnoreCase(Terminal.NEWLINE))
	            newLine(code);
	        else 
	        {
	            newlist.add(currentToken.getToken());
	            //i++;
	        }
	            
	        }
	    
	    
	    
	    
	}

